########################################################
# Project:    Talking to the Populist Radical Right
# Task:       The script merges the data sets with the
#             scores from the CA for the German parties, 
#             normalizes & annotates them
# Author:     Jan Schwalbach (21/07/2022)
########################################################

load(file="CA_D_ALL.Rdata")
load(file="CA_D_MIG.Rdata")
load(file="CA_D_ED.Rdata")

Positions_CA_D_ALL[,1] <- (Positions_CA_D_ALL[,1]-min(Positions_CA_D_ALL[,1]))/(max(Positions_CA_D_ALL[,1])-min(Positions_CA_D_ALL[,1]))
Positions_CA_D_ALL[,4] <- (Positions_CA_D_ALL[,4]-min(Positions_CA_D_ALL[,4]))/(max(Positions_CA_D_ALL[,4])-min(Positions_CA_D_ALL[,4]))
Positions_CA_D_ALL[,7] <- (Positions_CA_D_ALL[,7]-min(Positions_CA_D_ALL[,7]))/(max(Positions_CA_D_ALL[,7])-min(Positions_CA_D_ALL[,7]))

Positions_CA_D_MIG[,1] <- (Positions_CA_D_MIG[,1]-min(Positions_CA_D_MIG[,1]))/(max(Positions_CA_D_MIG[,1])-min(Positions_CA_D_MIG[,1]))
Positions_CA_D_MIG[,4] <- (Positions_CA_D_MIG[,4]-min(Positions_CA_D_MIG[,4]))/(max(Positions_CA_D_MIG[,4])-min(Positions_CA_D_MIG[,4]))
Positions_CA_D_MIG[,7] <- (Positions_CA_D_MIG[,7]-min(Positions_CA_D_MIG[,7]))/(max(Positions_CA_D_MIG[,7])-min(Positions_CA_D_MIG[,7]))

Positions_CA_D_ED[,1] <- (Positions_CA_D_ED[,1]-min(Positions_CA_D_ED[,1]))/(max(Positions_CA_D_ED[,1])-min(Positions_CA_D_ED[,1]))
Positions_CA_D_ED[,4] <- (Positions_CA_D_ED[,4]-min(Positions_CA_D_ED[,4]))/(max(Positions_CA_D_ED[,4])-min(Positions_CA_D_ED[,4]))
Positions_CA_D_ED[,7] <- (Positions_CA_D_ED[,7]-min(Positions_CA_D_ED[,7]))/(max(Positions_CA_D_ED[,7])-min(Positions_CA_D_ED[,7]))

CA_D_ALL_ALL <- Positions_CA_D_ALL[,1:3]
CA_D_ALL_GOV <- Positions_CA_D_ALL[,4:6]
CA_D_ALL_OPP <- Positions_CA_D_ALL[,7:9]

CA_D_MIG_ALL <- Positions_CA_D_MIG[,1:3]
CA_D_MIG_GOV <- Positions_CA_D_MIG[,4:6]
CA_D_MIG_OPP <- Positions_CA_D_MIG[,7:9]

CA_D_ED_ALL <- Positions_CA_D_ED[,1:3]
CA_D_ED_GOV <- Positions_CA_D_ED[,4:6]
CA_D_ED_OPP <- Positions_CA_D_ED[,7:9]

names(CA_D_ALL_ALL)[1] <- "Position"
names(CA_D_ALL_GOV)[1] <- "Position"
names(CA_D_ALL_OPP)[1] <- "Position"
names(CA_D_MIG_ALL)[1] <- "Position"
names(CA_D_MIG_GOV)[1] <- "Position"
names(CA_D_MIG_OPP)[1] <- "Position"
names(CA_D_ED_ALL)[1] <- "Position" 
names(CA_D_ED_GOV)[1] <- "Position" 
names(CA_D_ED_OPP)[1] <- "Position" 

names(CA_D_ALL_ALL)[2] <- "Position2"
names(CA_D_ALL_GOV)[2] <- "Position2"
names(CA_D_ALL_OPP)[2] <- "Position2"
names(CA_D_MIG_ALL)[2] <- "Position2"
names(CA_D_MIG_GOV)[2] <- "Position2"
names(CA_D_MIG_OPP)[2] <- "Position2"
names(CA_D_ED_ALL)[2] <- "Position2" 
names(CA_D_ED_GOV)[2] <- "Position2" 
names(CA_D_ED_OPP)[2] <- "Position2" 

# Calculating the difference to the challenger party

CA_D_ALL_ALL$challenger <- CA_D_ALL_ALL$Position[1]
CA_D_ALL_ALL$Difference <- CA_D_ALL_ALL$Position-CA_D_ALL_ALL$challenger
CA_D_ALL_ALL$Difference <- abs(CA_D_ALL_ALL$Difference)

CA_D_ALL_GOV$challenger <- CA_D_ALL_GOV$Position[1]
CA_D_ALL_GOV$Difference <- CA_D_ALL_GOV$Position-CA_D_ALL_GOV$challenger
CA_D_ALL_GOV$Difference <- abs(CA_D_ALL_GOV$Difference)

CA_D_ALL_OPP$challenger <- CA_D_ALL_OPP$Position[1]
CA_D_ALL_OPP$Difference <- CA_D_ALL_OPP$Position-CA_D_ALL_OPP$challenger
CA_D_ALL_OPP$Difference <- abs(CA_D_ALL_OPP$Difference)

CA_D_MIG_ALL$challenger <- CA_D_MIG_ALL$Position[1]
CA_D_MIG_ALL$Difference <- CA_D_MIG_ALL$Position-CA_D_MIG_ALL$challenger
CA_D_MIG_ALL$Difference <- abs(CA_D_MIG_ALL$Difference)

CA_D_MIG_GOV$challenger <- CA_D_MIG_GOV$Position[1]
CA_D_MIG_GOV$Difference <- CA_D_MIG_GOV$Position-CA_D_MIG_GOV$challenger
CA_D_MIG_GOV$Difference <- abs(CA_D_MIG_GOV$Difference)

CA_D_MIG_OPP$challenger <- CA_D_MIG_OPP$Position[1]
CA_D_MIG_OPP$Difference <- CA_D_MIG_OPP$Position-CA_D_MIG_OPP$challenger
CA_D_MIG_OPP$Difference <- abs(CA_D_MIG_OPP$Difference)

CA_D_ED_ALL$challenger <- CA_D_ED_ALL$Position[1]
CA_D_ED_ALL$Difference <- CA_D_ED_ALL$Position-CA_D_ED_ALL$challenger
CA_D_ED_ALL$Difference <- abs(CA_D_ED_ALL$Difference)

CA_D_ED_GOV$challenger <- CA_D_ED_GOV$Position[1]
CA_D_ED_GOV$Difference <- CA_D_ED_GOV$Position-CA_D_ED_GOV$challenger
CA_D_ED_GOV$Difference <- abs(CA_D_ED_GOV$Difference)

CA_D_ED_OPP$challenger <- CA_D_ED_OPP$Position[1]
CA_D_ED_OPP$Difference <- CA_D_ED_OPP$Position-CA_D_ED_OPP$challenger
CA_D_ED_OPP$Difference <- abs(CA_D_ED_OPP$Difference)

# Annotating government/opposition debates

CA_D_ALL_ALL$type <-"all"
CA_D_ALL_GOV$type <-"government"  
CA_D_ALL_OPP$type <-"opposition"
CA_D_MIG_ALL$type <-"all"
CA_D_MIG_GOV$type <-"government"  
CA_D_MIG_OPP$type <-"opposition"
CA_D_ED_ALL$type <-"all" 
CA_D_ED_GOV$type <-"government"   
CA_D_ED_OPP$type <-"opposition" 

# Annotating the matter of the debate

CA_D_ALL_ALL$matter <-"all"
CA_D_ALL_GOV$matter <-"all"  
CA_D_ALL_OPP$matter <-"all"
CA_D_MIG_ALL$matter <-"Migration"
CA_D_MIG_GOV$matter <-"Migration"  
CA_D_MIG_OPP$matter <-"Migration"
CA_D_ED_ALL$matter <-"Education" 
CA_D_ED_GOV$matter <-"Education"   
CA_D_ED_OPP$matter <-"Education" 

Positions_D <- rbind(CA_D_ALL_ALL,CA_D_ALL_GOV,CA_D_ALL_OPP,CA_D_MIG_ALL,CA_D_MIG_GOV,CA_D_MIG_OPP,CA_D_ED_ALL,CA_D_ED_GOV,CA_D_ED_OPP)
Positions_D$Party <- Positions_D$party

# left/right parties

Positions_D$left <- 0
Positions_D$left[Positions_D$Party == "GREENS"] <- 1
Positions_D$left[Positions_D$Party == "LEFT"] <- 1
Positions_D$left[Positions_D$Party == "SPD"] <- 1
Positions_D$country <- "D"

# mainstream parties

Positions_D$mainstream <- 0
Positions_D$mainstream[Positions_D$Party == "SPD"] <- 1
Positions_D$mainstream[Positions_D$Party == "CDU/CSU"] <- 1

# government/opposition parties

Positions_D$year <- 2017
Positions_D$Government <- "Opposition"

Positions_D$Government[Positions_D$Party == "SPD"] <- "Government"
Positions_D$Government[Positions_D$Party == "CDU/CSU"] <- "Government"

save(Positions_D, file = "Positions_D_CA_normal.Rdata") # save corpus
